<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="游戏名称" prop="gameName">
                <el-input
                        v-model="queryParams.gameName"
                        placeholder="请输入游戏名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="游戏类型" prop="gameCode">
                <el-select v-model="queryParams.gameType" placeholder="请选择游戏类型" clearable size="small">
                    <el-option
                            v-for="game in gameType"
                            :key="game.value"
                            :label="game.label"
                            :value="game.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['app:game:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['app:game:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['app:game:remove']"
                >删除
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="gameList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" type="index" width="50" align="center">
                <template slot-scope="scope">
                    <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="图标" align="center" width="80" prop="gameLogo" >
                <template scope="scope">
                    <img :src="scope.row.gameLogo" style="width:60px;">
                </template>
            </el-table-column>
            <el-table-column label="游戏名称" align="center" prop="gameName"/>
            <el-table-column label="游戏类型" align="center" prop="gameType" width="100">
                <template slot-scope="scope">
                    <el-tag   v-if="scope.row.gameType==1">安卓</el-tag>
                    <el-tag  effect="dark" type="success" v-if="scope.row.gameType==2">IOS</el-tag>
                    <el-tag  effect="dark" type="info" v-if="scope.row.gameType==3">H5</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="显示" align="center" prop="gameState">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.gameState" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['app:game:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['app:game:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改游戏对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="游戏类型" prop="gameType">
                    <el-select v-model="form.gameType" placeholder="请选择游戏类型" clearable size="small">
                        <el-option
                                v-for="game in gameType"
                                :key="game.value"
                                :label="game.label"
                                :value="game.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="游戏名称" prop="gameName">
                    <el-input v-model="form.gameName" placeholder="请输入游戏名称"/>
                </el-form-item>
                <el-form-item label="排序" prop="orderNum">
                    <el-input-number v-model="form.orderNum"  :min="1" :max="100" label="排序"></el-input-number>
                </el-form-item>
                <el-form-item label="游戏图标">
                    <ImageUpload v-if="form.gameId" v-model="form.gameLogo"/>
                    <ImageUpload v-if="!form.gameId" v-model="form.gameLogo"/>
                </el-form-item>
                <el-form-item label="游戏地址" prop="gameUrl">
                    <el-input v-model="form.gameUrl" placeholder="请输入游戏地址"/>
                </el-form-item>
                <el-form-item label="描述" prop="gameDesc">
                    <el-input type="textarea" v-model="form.gameDesc" placeholder="请输入描述" maxlength="200" show-word-limit />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listGame, getGame, delGame, addGame, updateGame, exportGame,changeGameState } from '@/api/app/game'
import ImageUpload from '@/components/ImageUpload';
import { changeRoleStatus } from '@/api/system/role'
export default {
    name: 'game',
    components: {ImageUpload},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 游戏表格数据
            gameList: [],
            gameType: [{value:1,label:'安卓'},{value:2,label:'IOS'},{value:3,label:'H5'}],
            // 弹出层标题
            title: '',
            baseUrl: 'https://youhuiniu.oss-cn-beijing.aliyuncs.com/',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gameName: null,
                gameCode: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                gameName: [
                    { required: true, message: '游戏名称不能为空', trigger: 'blur' }
                ],
                gameType: [
                    { required: true, message: '游戏类型不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询游戏列表 */
        getList() {
            this.loading = true
            listGame(this.queryParams).then(response => {
                this.gameList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                gameId: null,
                gameName: null,
                gameCode: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.gameId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加游戏'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const gameId = row.gameId || this.ids
            getGame(gameId).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改游戏'
            })
        },
        // 角色状态修改
        handleStatusChange(row) {
            changeGameState(row.gameId, row.gameState);
            this.msgSuccess('操作' + this.$t('constant.success'));
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.gameId != null) {
                        updateGame(this.form).then(response => {
                            this.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addGame(this.form).then(response => {
                            this.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const gameIds = row.gameId || this.ids
            this.$confirm('是否确认删除游戏编号为"' + gameIds + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return delGame(gameIds)
            }).then(() => {
                this.getList()
                this.msgSuccess('删除成功')
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams
            this.$confirm('是否确认导出所有游戏数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                return exportGame(queryParams)
            }).then(response => {
                this.download(response.msg)
            })
        }
    }
}
</script>
